package ace.cmp.data.methodname.sql.mybatisplus.core.method;

import ace.cmp.data.methodname.method.name.sql.core.util.AssertUtils;
import ace.cmp.data.methodname.method.name.sql.entity.model.EntityMapping;
import ace.cmp.data.methodname.method.name.sql.entity.resolver.EntityMethodNameToQueryResolver;
import ace.cmp.data.methodname.method.name.sql.entity.resolver.impl.EntityMethodNameToQueryResolverImpl;
import ace.cmp.data.methodname.sql.mybatisplus.core.properties.MybatisPlugMethodNameToSqlProperties;
import ace.cmp.data.methodname.sql.mybatisplus.core.util.EntityMappingUtils;
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import java.lang.reflect.Method;
import lombok.extern.slf4j.Slf4j;

/**
 * @author caspar
 * @date 2023/2/16 14:13 从方法名，推导sql实现类
 */
@Slf4j
public abstract class AbstractMethodNameToSqlMethod extends AbstractMethod {

  protected final Method method;
  protected final TableInfo tableInfo;
  protected final Class mapperClass;
  protected final MybatisPlugMethodNameToSqlProperties sqlProperties;

  /**
   * @param method      方法名
   * @param tableInfo   表映射
   * @param mapperClass 接口类型
   * @since 3.5.0
   */
  public AbstractMethodNameToSqlMethod(
      Method method,
      TableInfo tableInfo,
      Class mapperClass,
      MybatisPlugMethodNameToSqlProperties sqlProperties) {
    super(method.getName());
    AssertUtils.notNull(method, "method must not be null");
    AssertUtils.notNull(tableInfo, "tableInfo must not be null");
    AssertUtils.notNull(mapperClass, "mapperClass must not be null");
    AssertUtils.notNull(sqlProperties, "sqlProperties must not be null");
    this.method = method;
    this.tableInfo = tableInfo;
    this.mapperClass = mapperClass;
    this.sqlProperties = sqlProperties;
  }

  protected EntityMethodNameToQueryResolver getEntityQueryResolver() {
    EntityMapping entityMapping = this.getEntityMapping();
    EntityMethodNameToQueryResolver entityQueryResolver =
        new EntityMethodNameToQueryResolverImpl(this.method, entityMapping);
    return entityQueryResolver;
  }

  protected EntityMapping getEntityMapping() {
    return EntityMappingUtils.getEntityMapping(this.tableInfo);
  }
}
